package com.scitc.pleasurevideo.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.scitc.pleasurevideo.R;

/**
 * <p>
 * Title: CustomDialog
 * </p>
 * <p>
 * Description:自定义Dialog（参数传入Dialog样式文件，Dialog布局文件）
 * </p>
 * <p>
 * Copyright: Copyright (c) 2013
 * </p>
 *
 * @author archie
 * @version 1.0
 */
public class SwDialog extends Dialog {
    int layoutRes = R.layout.dialog_sw;// 布局文件

    Context context;

    Activity mActivity;

    private DialogListener okListener, cancelListener;

    private String title, text;

    private TextView titleText, subtitleText, okText;

    private boolean isNotClose = false;

    public SwDialog(Context context, String title, String text) {
        super(context, R.style.customMarketScoresDailogStyle);
        this.context = context;
        this.title = title;
        this.text = text;
        mActivity = (Activity) context;
    }

    public void setNotClose(boolean isNotClose) {
        this.isNotClose = isNotClose;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(layoutRes);
        titleText = (TextView) findViewById(R.id.titleText);
        subtitleText = (TextView) findViewById(R.id.subtitleText);
        okText = (TextView) findViewById(R.id.okText);

        // 根据id在布局中找到控件对象
        View closeBtn = findViewById(R.id.cancelBtn);
        View openBtn = findViewById(R.id.okBtn);
        closeBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isNotClose) {
                    if (cancelListener != null) {
                        cancelListener.handle();
                    }
                    return;
                }
                dismiss();
                if (cancelListener != null) {
                    cancelListener.handle();
                }
            }
        });
        openBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
                if (okListener != null) {
                    okListener.handle();
                }
            }
        });
        titleText.setText(title);
        subtitleText.setText(text);
        this.setCanceledOnTouchOutside(false);
    }

    public void setOkText(String okStr) {
        if (okText != null) {
            okText.setText(okStr);
        }
    }

    public DialogListener getOkListener() {
        return okListener;
    }

    public void setOkListener(DialogListener okListener) {
        this.okListener = okListener;
    }

    public DialogListener getCancelListener() {
        return cancelListener;
    }

    public void setCancelListener(DialogListener cancelListener) {
        this.cancelListener = cancelListener;
    }

    public static interface DialogListener {
        public void handle();
    }
}
